Trò chơi game bắn súng đơn giản trong UNITY Engine
23.627 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.UI;
4 using UnityEngine.SceneManagement;
5
6 public class UIgame : MonoBehaviour
7 {
8 SoundManager sounds;
9 int score;//actual score
10 int maximumScore;
11 public static bool scoreAchived;
12
13 public Text scoreText;
14 public Text informationText;
15 public Text muteText;
16
17 public GameObject PauseScreen;
18 public GameObject OptionsScreen;
19 public GameObject GameScreen;
20 public GameObject FailScreen;
21 public GameObject WinScreen;
22
23 public Sprite[] hearts;
24 public Image lifeBar;
25
26 void Awake ()
27 {
28 sounds = GameObject.FindObjectOfType<SoundManager>();
29 }
30
31 void Start()
32 {
33 if (SystemScr.difficultyIsHard)
34 maximumScore = 2000;
35 else
36 maximumScore = 1000;
37
38 scoreText.text = score + "/" + maximumScore;
39 scoreAchived = false;
40
41 if (SystemScr.mute)
42 muteText.text = "ON";
43 }
44
45 public void IncreaseScore(int _score)
46 {
47 score += _score;
48 scoreText.text = score + "/" + maximumScore;
49 ShowInformation("+"+_score.ToString());
50
51 if (score >= maximumScore)
52 {
53 scoreAchived = true;
54 }
55
56 }
57
58 //drop fail screen when lives = 0
59 public void ShowLives(int lives)
60 {
61 if (lives <= 0)
62 {
63 lifeBar.enabled = false;
64 FailScreen.SetActive(true);
65 return;
66 }
67 else
68 lifeBar.sprite = hearts[lives - 1];
69 }
70
71 //boss die
72 public void Win()
73 {
74 GameObject.Find("Player").SetActive(false); ;
75 StartCoroutine("WinRoutine");
76 }
77
78 IEnumerator WinRoutine()
79 {
80 yield return new WaitForSeconds(1);
81 WinScreen.SetActive(true);
82 }
83
84 public void ShowInformation(string info)
85 {
86 informationText.text = info;
87 StopCoroutine("ShowInfoForSomeTime");
88 StartCoroutine("ShowInfoForSomeTime");
89 }
90
91 IEnumerator ShowInfoForSomeTime()
92 {
93 informationText.enabled = true;
94 yield return new WaitForSeconds(1);
95 informationText.enabled = false;
96 }
97
98
99 //BUTTONS METHODS
100 public void Pause_btn()
101 {
102 if (SystemScr.paused)
103 {
104 Resume_btn();
105 }
106 else
107 {
108 sounds.PlaySoundsUI(false);
109
110 SystemScr.Pause(true);
111 GameScreen.SetActive(false);
112 PauseScreen.SetActive(true);
113 }
114 }
115
116 public void Resume_btn()
117 {
118 sounds.PlaySoundsUI(false);
119
120 SystemScr.Pause(false);
121 GameScreen.SetActive(true);
122 PauseScreen.SetActive(false);
123 OptionsScreen.SetActive(false);
124 }
125
126 public void Options_btn()
127 {
128 sounds.PlaySoundsUI(true);
129
130 OptionsScreen.SetActive(true);
131 }
132
133 public void ExitFromPauseToMainMenu_btn()
134 {
135 SystemScr.Pause(false);
136 SceneManager.LoadScene("MenuScene");
137 }
138
139 public void ExitFromOptions_btn()
140 {
141 sounds.PlaySoundsUI(true);
142
143 OptionsScreen.SetActive(false);
144 }
145
146 public void Mute_btn()
147 {
148 if (SystemScr.mute)
149 {
150 sounds.PlaySoundsUI(true);
151
152 muteText.text = "OFF";
153 SystemScr.mute = false;
154
155 sounds.PlayMusic(true);
156 }
157 else
158 {
159 muteText.text = "ON";
160 SystemScr.mute = true;
161
162 sounds.PlayMusic(false);
163 }
164 }
165
166 public void Retry_btn()
167 {
168 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
169 }
170 }
2 using System.Collections;
3 using UnityEngine.UI;
4 using UnityEngine.SceneManagement;
5
6 public class UIgame : MonoBehaviour
7 {
8 SoundManager sounds;
9 int score;//actual score
10 int maximumScore;
11 public static bool scoreAchived;
12
13 public Text scoreText;
14 public Text informationText;
15 public Text muteText;
16
17 public GameObject PauseScreen;
18 public GameObject OptionsScreen;
19 public GameObject GameScreen;
20 public GameObject FailScreen;
21 public GameObject WinScreen;
22
23 public Sprite[] hearts;
24 public Image lifeBar;
25
26 void Awake ()
27 {
28 sounds = GameObject.FindObjectOfType<SoundManager>();
29 }
30
31 void Start()
32 {
33 if (SystemScr.difficultyIsHard)
34 maximumScore = 2000;
35 else
36 maximumScore = 1000;
37
38 scoreText.text = score + "/" + maximumScore;
39 scoreAchived = false;
40
41 if (SystemScr.mute)
42 muteText.text = "ON";
43 }
44
45 public void IncreaseScore(int _score)
46 {
47 score += _score;
48 scoreText.text = score + "/" + maximumScore;
49 ShowInformation("+"+_score.ToString());
50
51 if (score >= maximumScore)
52 {
53 scoreAchived = true;
54 }
55
56 }
57
58 //drop fail screen when lives = 0
59 public void ShowLives(int lives)
60 {
61 if (lives <= 0)
62 {
63 lifeBar.enabled = false;
64 FailScreen.SetActive(true);
65 return;
66 }
67 else
68 lifeBar.sprite = hearts[lives - 1];
69 }
70
71 //boss die
72 public void Win()
73 {
74 GameObject.Find("Player").SetActive(false); ;
75 StartCoroutine("WinRoutine");
76 }
77
78 IEnumerator WinRoutine()
79 {
80 yield return new WaitForSeconds(1);
81 WinScreen.SetActive(true);
82 }
83
84 public void ShowInformation(string info)
85 {
86 informationText.text = info;
87 StopCoroutine("ShowInfoForSomeTime");
88 StartCoroutine("ShowInfoForSomeTime");
89 }
90
91 IEnumerator ShowInfoForSomeTime()
92 {
93 informationText.enabled = true;
94 yield return new WaitForSeconds(1);
95 informationText.enabled = false;
96 }
97
98
99 //BUTTONS METHODS
100 public void Pause_btn()
101 {
102 if (SystemScr.paused)
103 {
104 Resume_btn();
105 }
106 else
107 {
108 sounds.PlaySoundsUI(false);
109
110 SystemScr.Pause(true);
111 GameScreen.SetActive(false);
112 PauseScreen.SetActive(true);
113 }
114 }
115
116 public void Resume_btn()
117 {
118 sounds.PlaySoundsUI(false);
119
120 SystemScr.Pause(false);
121 GameScreen.SetActive(true);
122 PauseScreen.SetActive(false);
123 OptionsScreen.SetActive(false);
124 }
125
126 public void Options_btn()
127 {
128 sounds.PlaySoundsUI(true);
129
130 OptionsScreen.SetActive(true);
131 }
132
133 public void ExitFromPauseToMainMenu_btn()
134 {
135 SystemScr.Pause(false);
136 SceneManager.LoadScene("MenuScene");
137 }
138
139 public void ExitFromOptions_btn()
140 {
141 sounds.PlaySoundsUI(true);
142
143 OptionsScreen.SetActive(false);
144 }
145
146 public void Mute_btn()
147 {
148 if (SystemScr.mute)
149 {
150 sounds.PlaySoundsUI(true);
151
152 muteText.text = "OFF";
153 SystemScr.mute = false;
154
155 sounds.PlayMusic(true);
156 }
157 else
158 {
159 muteText.text = "ON";
160 SystemScr.mute = true;
161
162 sounds.PlayMusic(false);
163 }
164 }
165
166 public void Retry_btn()
167 {
168 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
169 }
170 }
int score;actual score
drop fail screen when lives = 0
boss die
BUTTONS METHODS